package jp.co.piratealliance.pirateAllianceWeb.service.order.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jp.co.piratealliance.pirateAllianceWeb.dao.coins.CoinsDao;
import jp.co.piratealliance.pirateAllianceWeb.dao.com.UserDao;
import jp.co.piratealliance.pirateAllianceWeb.dao.order.MessageDao;
import jp.co.piratealliance.pirateAllianceWeb.dao.order.OrderDao;
import jp.co.piratealliance.pirateAllianceWeb.def.Constants;
import jp.co.piratealliance.pirateAllianceWeb.dto.coins.GoldenCoinDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.coins.GoldenCoinHistoryDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.coins.SilverCoinDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.coins.SilverCoinHistoryDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.com.UserInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.order.EvaluationInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.order.EventInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.order.MessageInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.order.OrderHisInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.order.OrderInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.order.ScheduleInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.order.ServiceGoodsOrderDetailInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.helper.CommonHelper;
import jp.co.piratealliance.pirateAllianceWeb.service.order.OrderService;


/**
*
* copyright   pirate alliance team 2014
* @author     lunxing
*
*/

public class OrderServiceImpl implements OrderService {

	private OrderDao orderDao;

	private MessageDao messageDao;

	private UserDao userDao;

	private CoinsDao coinsDao;

	public OrderInfoDto getOrdersByUserIdAndGoodsIdOne(String user_id, String goods_id, String language_id) {
		Map<String,Object> paramMap = new HashMap<String,Object>();

		paramMap.put("user_id", user_id);
		paramMap.put("goods_id", goods_id);
		paramMap.put("language_id", language_id);
		List<OrderInfoDto> orderInfoList = orderDao.selectOrdersByUserIdAndGoodsId(paramMap);

		if (orderInfoList != null && orderInfoList.size() > 0) {
			return orderInfoList.get(0);
		} else {
			return null;
		}
	}

	public List<ServiceGoodsOrderDetailInfoDto> getServiceGoodsOrderDetailByOrderId(String order_id) {
		return orderDao.selectServiceGoodsOrderDetailByOrderId(order_id);
	}

	public List<ScheduleInfoDto> getScheduleByOrderId(String order_id) {
		return orderDao.selectScheduleByOrderId(order_id);
	}

	public List<EventInfoDto> getEventByScheduleId(String schedule_id) {
		return orderDao.selectEventByScheduleId(schedule_id);
	}

	public List<EventInfoDto> getEventByOrderId(String order_id) {
		return orderDao.selectEventByOrderId(order_id);
	}

	public int insertOrder(OrderInfoDto orderInfoDto) {
		return orderDao.insertOrder(orderInfoDto);
	}

	public int insertServiceGoodsOrderDetail(ServiceGoodsOrderDetailInfoDto serviceGoodsOrderDetailInfoDto) {
		return orderDao.insertServiceGoodsOrderDetail(serviceGoodsOrderDetailInfoDto);
	}

	public int insertSchedule(ScheduleInfoDto scheduleInfoDto) {
		return orderDao.insertSchedule(scheduleInfoDto);
	}

	public int insertEvent(EventInfoDto eventInfoDto) {
		return orderDao.insertEvent(eventInfoDto);
	}

	public int updateOrder(OrderInfoDto orderInfoDto, MessageInfoDto messageInfoDto) {
		if(messageInfoDto != null)
		{
			messageDao.insertMessage(messageInfoDto);
		}

		if(Constants.ORDER_STATUS_OK == orderInfoDto.getStatus())
		{
			UserInfoDto userInfo = userDao.selectUserByUserId(orderInfoDto.getUser_id());

			String goldencoins = CommonHelper.getGoldenCoinsForOrder();
			GoldenCoinHistoryDto golderCoinHisDto = new GoldenCoinHistoryDto();
			golderCoinHisDto.setGolden_coin_history_id(CommonHelper.getSequence(Constants.TB_GOLDEN_COIN_HISTORY));
			golderCoinHisDto.setGolden_coin_id(userInfo.getGolden_coin_id());
			golderCoinHisDto.setGolden_coin_rule_id(Constants.GOLDEN_COIN_RULE_ID);
			golderCoinHisDto.setCoin(goldencoins);
			coinsDao.insertGoldenCoinHistory(golderCoinHisDto);

			GoldenCoinDto goldenCoin = new GoldenCoinDto();
			goldenCoin.setGolden_coin_id(userInfo.getGolden_coin_id());
			goldenCoin.setAccountbalance(goldencoins);
			coinsDao.updateGoldenCoin(goldenCoin);

			String silverCoins = CommonHelper.getSilverCoinsForOrder();
			SilverCoinHistoryDto silverCoinHisDto = new SilverCoinHistoryDto();
			silverCoinHisDto.setSilver_coin_history_id(CommonHelper.getSequence(Constants.TB_SILVER_COIN_HISTORY));
			silverCoinHisDto.setSilver_coin_id(userInfo.getSilver_coin_id());
			silverCoinHisDto.setSilver_coin_rule_id(Constants.SILVER_COIN_RULE_ID);
			silverCoinHisDto.setCoin(silverCoins);
			coinsDao.insertSilverCoinHistory(silverCoinHisDto);

			SilverCoinDto silverCoin = new SilverCoinDto();
			silverCoin.setSilver_coin_id(userInfo.getSilver_coin_id());
			silverCoin.setAccountbalance(silverCoins);
			coinsDao.updateSilverCoin(silverCoin);
		}
		orderInfoDto.setUser_id(null);
		return orderDao.updateOrder(orderInfoDto);
	}

	public int updateOrder(OrderInfoDto orderInfoDto) {
		return orderDao.updateOrder(orderInfoDto);
	}

	public int updateServiceGoodsOrderDetail(ServiceGoodsOrderDetailInfoDto serviceGoodsOrderDetailInfoDto) {
		return orderDao.updateServiceGoodsOrderDetail(serviceGoodsOrderDetailInfoDto);
	}

	public int updateSchedule(ScheduleInfoDto scheduleInfoDto) {
		return orderDao.updateSchedule(scheduleInfoDto);
	}

	public int updateEvent(EventInfoDto eventInfoDto) {
		return orderDao.updateEvent(eventInfoDto);
	}

	public List<OrderHisInfoDto> selectOrderHis(String userId, String status)
	{
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("user_id", userId);
		param.put("status", status);

		return orderDao.selectOrderHisPaging(param);
	}

	public int deleteScheduleByOrderId(String order_id) {
		return orderDao.deleteScheduleByOrderId(order_id);
	}

	public int deleteEventByScheduleId(String schedule_id) {
		return orderDao.deleteEventByScheduleId(schedule_id);
	}

	public int deleteEventByOrderId(String order_id) {
		return orderDao.deleteEventByOrderId(order_id);
	}

	public List<EvaluationInfoDto> getEvaluationInfoListByGoodsId(String goods_id) {
		// TODO Auto-generated method stub
		return orderDao.selectEvaluationInfoList(goods_id);
	}

	/**
	 * @return the orderDao
	 */
	public OrderDao getOrderDao() {
		return orderDao;
	}

	/**
	 * @param orderDao the orderDao to set
	 */
	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}

	public MessageDao getMessageDao()
	{
		return messageDao;
	}

	public void setMessageDao(MessageDao messageDao)
	{
		this.messageDao = messageDao;
	}

	public UserDao getUserDao()
	{
		return userDao;
	}

	public void setUserDao(UserDao userDao)
	{
		this.userDao = userDao;
	}

	public CoinsDao getCoinsDao()
	{
		return coinsDao;
	}

	public void setCoinsDao(CoinsDao coinsDao)
	{
		this.coinsDao = coinsDao;
	}

	public Integer getAverageEvaluation(String goods_id) {
		return orderDao.selectAverageEvaluation(goods_id);
	}


}
